package lion.crawl.fetch;

import java.io.IOException;

import lion.dev.io.FileUtil;

import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
 * 用httpclient来处理url的下载，可以传入一个已经初始化好的(如登录过)httpclient。
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2014年5月12日
 */
public class HttpClientDownloader implements IDownloader {

	private CloseableHttpClient httpClient;

	public HttpClientDownloader(CloseableHttpClient httpClienth) {

		this.httpClient = httpClienth;
	}

	public HttpClientDownloader() {

		this.httpClient = HttpClients.createDefault();
	}

	@Override
	public Content fatch(String url) throws IOException {

		HttpGet get = new HttpGet(url);

		CloseableHttpResponse response = this.httpClient.execute(get);
		Content content = new Content();
		for (Header header : response.getAllHeaders()) {
			content.setHeader(header.getName(), header.getValue());
		}
		content.setContent(EntityUtils.toByteArray(response.getEntity()));

		return content;
	}

	@Override
	public String curl(String url) throws IOException {

		return FileUtil.read(fatch(url).getContent());
	}

	@Override
	protected void finalize() throws Throwable {

		HttpClientUtils.closeQuietly(httpClient);
		this.httpClient = null;

		super.finalize();
	}
}
